package icedot.library.common.utils.struct;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import icedot.library.common.base.Logger;

public class ContactInfo
{
	public ContactInfo()
	{
		_id = "";
		_name = "";
		_phoneList = new ArrayList<>();
	}
	public String 				_id;
	public String 				_name;		//姓名
	public List<PhoneInfo> 		_phoneList;		//电话

	public String jsonString(String type)
	{
		try
		{
			for (int i = 0; i < _phoneList.size(); ++i) {
				if (_phoneList.get(i)._type.equals(type)) {
					JSONObject jsonObj = new JSONObject();
					jsonObj.put("id", _id);
					jsonObj.put("name", _name);
					jsonObj.put("phone", _phoneList.get(i)._phone);
					jsonObj.put("type", _phoneList.get(i)._type);
					return jsonObj.toString();
				}
			}
			return null;
		}
		catch (Exception e)
		{
			Logger.showDebugMsg(Logger.s_error,e.getClass().getName(),e.getMessage());
			return null;
		}
	}

	public String jsonString()
	{
		try
		{
			StringBuilder ret = new StringBuilder();
			for(int i=0;i < _phoneList.size();++i)
			{
				JSONObject jsonObj = new JSONObject();
				jsonObj.put("id",_id);
				jsonObj.put("name",_name);
				jsonObj.put("phone",_phoneList.get(i)._phone);
				jsonObj.put("type",_phoneList.get(i)._type);
				if(ret.length() == 0)
				{
					ret.append(jsonObj.toString());
				}
				else
				{
					ret.append("," + jsonString().toString());
				}
			}
			return ret.toString();
		}
		catch (Exception e)
		{
			Logger.showDebugMsg(Logger.s_error,"Exception",e.getMessage());
			return "";
		}

	}
}
